/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.property.Acl;
/*     */ import com.zimbra.cs.dav.property.CalDavProperty;
/*     */ import com.zimbra.cs.dav.property.CardDavProperty;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.ACL.Grant;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class User
/*     */   extends Principal
/*     */ {
/*     */   public User(DavContext ctxt, Account account, String url)
/*     */     throws ServiceException
/*     */   {
/*  60 */     super(account, url);
/*  61 */     this.mAccount = account;
/*  62 */     String user = getOwner();
/*  63 */     addProperty(CalDavProperty.getCalendarHomeSet(user));
/*  64 */     addProperty(CalDavProperty.getCalendarUserType(this));
/*  65 */     addProperty(CalDavProperty.getScheduleInboxURL(user));
/*  66 */     addProperty(CalDavProperty.getScheduleOutboxURL(user));
/*  67 */     if ((ctxt.getAuthAccount().equals(account)) && 
/*  68 */       (ctxt.useIcalDelegation())) {
/*  69 */       addProperty(new CalendarProxyReadFor(this.mAccount));
/*  70 */       addProperty(new CalendarProxyWriteFor(this.mAccount));
/*  71 */       addProperty(new ProxyGroupMembership());
/*     */     }
/*     */     
/*  74 */     addProperty(Acl.getPrincipalUrl(this));
/*  75 */     ArrayList<String> addrs = new ArrayList();
/*  76 */     for (String addr : account.getMailDeliveryAddress())
/*  77 */       addrs.add(addr);
/*  78 */     for (String alias : account.getMailAlias())
/*  79 */       addrs.add(alias);
/*  80 */     String principalAddr = UrlNamespace.getPrincipalUrl(account);
/*  81 */     if (principalAddr.endsWith("/")) {
/*  82 */       principalAddr = principalAddr.substring(0, principalAddr.length() - 1);
/*     */     }
/*  84 */     addrs.add(principalAddr);
/*  85 */     addProperty(CalDavProperty.getCalendarUserAddressSet(addrs));
/*  86 */     addProperty(CardDavProperty.getAddressbookHomeSet(user));
/*  87 */     setProperty(DavElements.E_HREF, url);
/*  88 */     String cn = account.getAttr("cn");
/*  89 */     if (cn == null)
/*  90 */       cn = account.getName();
/*  91 */     setProperty(DavElements.E_DISPLAYNAME, cn);
/*  92 */     this.mUri = url;
/*     */   }
/*     */   
/*     */   public Collection<DavResource> getChildren(DavContext ctxt) throws DavException
/*     */   {
/*  97 */     ArrayList<DavResource> proxies = new ArrayList();
/*  98 */     if (ctxt.useIcalDelegation()) {
/*     */       try {
/* 100 */         proxies.add(new CalendarProxyRead(getOwner(), this.mUri));
/* 101 */         proxies.add(new CalendarProxyWrite(getOwner(), this.mUri));
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     }
/* 105 */     return proxies;
/*     */   }
/*     */   
/*     */   public void delete(DavContext ctxt) throws DavException
/*     */   {
/* 110 */     throw new DavException("cannot delete this resource", 403, null);
/*     */   }
/*     */   
/*     */   public boolean isCollection()
/*     */   {
/* 115 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 166 */   private static QName[] SUPPORTED_REPORTS = { DavElements.E_ACL_PRINCIPAL_PROP_SET, DavElements.E_PRINCIPAL_MATCH, DavElements.E_PRINCIPAL_PROPERTY_SEARCH, DavElements.E_PRINCIPAL_SEARCH_PROPERTY_SET, DavElements.E_EXPAND_PROPERTY };
/*     */   private static final String CALENDAR_PROXY_READ = "calendar-proxy-read";
/*     */   private static final String CALENDAR_PROXY_WRITE = "calendar-proxy-write";
/*     */   
/*     */   public void patchProperties(DavContext ctxt, Collection<Element> set, Collection<QName> remove)
/*     */     throws DavException, IOException
/*     */   {}
/*     */   
/*     */   protected QName[] getSupportedReports()
/*     */   {
/* 176 */     return SUPPORTED_REPORTS;
/*     */   }
/*     */   
/*     */   private class CalendarProxyRead
/*     */     extends Principal
/*     */   {
/*     */     public CalendarProxyRead(String user, String url) throws ServiceException
/*     */     {
/* 184 */       super(url + "calendar-proxy-read");
/* 185 */       addResourceType(DavElements.E_CALENDAR_PROXY_READ);
/* 186 */       addProperty(Acl.getPrincipalUrl(this));
/* 187 */       addProperty(new User.ProxyGroupMemberSet(User.this, true));
/*     */     }
/*     */     
/*     */     public boolean isCollection() {
/* 191 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   private class CalendarProxyWrite extends Principal {
/*     */     public CalendarProxyWrite(String user, String url) throws ServiceException {
/* 197 */       super(url + "calendar-proxy-write");
/* 198 */       addResourceType(DavElements.E_CALENDAR_PROXY_WRITE);
/* 199 */       addProperty(Acl.getPrincipalUrl(this));
/* 200 */       addProperty(new User.ProxyGroupMemberSet(User.this, false));
/*     */     }
/*     */     
/*     */     public boolean isCollection() {
/* 204 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   private class ProxyProperty
/*     */     extends ResourceProperty {
/* 210 */     public ProxyProperty(QName name) { super(); }
/*     */     
/*     */     protected ArrayList<Pair<Mountpoint, ZFolder>> getMountpoints(DavContext ctxt) {
/* 213 */       ArrayList<Pair<Mountpoint, ZFolder>> mps = new ArrayList();
/*     */       try {
/* 215 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(User.this.mAccount);
/* 216 */         for (MailItem item : mbox.getItemList(ctxt.getOperationContext(), MailItem.Type.MOUNTPOINT)) {
/* 217 */           Mountpoint mp = (Mountpoint)item;
/*     */           
/* 219 */           if ((mp.getDefaultView() == MailItem.Type.APPOINTMENT) || (mp.getDefaultView() == MailItem.Type.TASK))
/*     */           {
/*     */ 
/* 222 */             ZAuthToken zat = AuthProvider.getAuthToken(ctxt.getAuthAccount()).toZAuthToken();
/* 223 */             ZMailbox zmbx = RemoteCollection.getRemoteMailbox(zat, mp.getOwnerId());
/*     */             
/* 225 */             if (zmbx != null)
/*     */             {
/*     */               try
/*     */               {
/* 229 */                 ZFolder folder = zmbx.getFolderById(mp.getTarget().toString(User.this.mAccount));
/*     */                 
/* 231 */                 if (folder != null)
/*     */                 {
/*     */ 
/* 234 */                   mps.add(new Pair(mp, folder)); }
/*     */               } catch (ServiceException se) {
/* 236 */                 ZimbraLog.dav.warn("can't get remote folder", se);
/*     */               } }
/*     */           }
/*     */         }
/* 240 */       } catch (ServiceException se) { ZimbraLog.dav.warn("can't get mailbox", se);
/*     */       }
/* 242 */       return mps;
/*     */     }
/*     */   }
/*     */   
/*     */   private class ProxyGroupMembership extends User.ProxyProperty {
/*     */     public ProxyGroupMembership() {
/* 248 */       super(DavElements.E_GROUP_MEMBERSHIP);
/* 249 */       setProtected(true);
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly) {
/* 253 */       Element group = super.toElement(ctxt, parent, true);
/* 254 */       if (nameOnly)
/* 255 */         return group;
/* 256 */       ArrayList<Pair<Mountpoint, ZFolder>> mps = getMountpoints(ctxt);
/* 257 */       for (Pair<Mountpoint, ZFolder> folder : mps) {
/*     */         try {
/* 259 */           short rights = ACL.stringToRights(((ZFolder)folder.getSecond()).getEffectivePerms());
/* 260 */           if ((rights & 0x2) > 0) {
/* 261 */             Account owner = Provisioning.getInstance().get(Key.AccountBy.id, ((Mountpoint)folder.getFirst()).getOwnerId());
/* 262 */             if (owner != null)
/* 263 */               group.addElement(DavElements.E_HREF).setText(UrlNamespace.getPrincipalUrl(User.this.mAccount, owner) + "calendar-proxy-write");
/* 264 */           } else if ((rights & 0x1) > 0) {
/* 265 */             Account owner = Provisioning.getInstance().get(Key.AccountBy.id, ((Mountpoint)folder.getFirst()).getOwnerId());
/* 266 */             if (owner != null)
/* 267 */               group.addElement(DavElements.E_HREF).setText(UrlNamespace.getPrincipalUrl(User.this.mAccount, owner) + "calendar-proxy-read");
/*     */           }
/*     */         } catch (ServiceException se) {
/* 270 */           ZimbraLog.dav.warn("can't convert rights", se);
/*     */         }
/*     */       }
/* 273 */       return group;
/*     */     } }
/*     */   
/*     */   private class ProxyGroupMemberSet extends ResourceProperty { private final boolean mReadOnly;
/*     */     
/* 278 */     public ProxyGroupMemberSet(boolean readOnly) { super();
/* 279 */       this.mReadOnly = readOnly;
/* 280 */       setProtected(true);
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 285 */       Element group = super.toElement(ctxt, parent, nameOnly);
/* 286 */       if (nameOnly)
/* 287 */         return group;
/*     */       try {
/* 289 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(User.this.mAccount);
/* 290 */         Folder f = mbox.getFolderById(ctxt.getOperationContext(), 10);
/* 291 */         if (f == null)
/* 292 */           return group;
/* 293 */         ACL acl = f.getEffectiveACL();
/* 294 */         if (acl == null)
/* 295 */           return group;
/* 296 */         for (ACL.Grant g : acl.getGrants())
/* 297 */           if (g.getGranteeType() == 1)
/*     */           {
/* 299 */             boolean match = ((g.getGrantedRights() & 0x1) != 0) && ((g.getGrantedRights() & 0x2) == 0);
/*     */             
/*     */ 
/* 302 */             if (match) {
/* 303 */               Account user = Provisioning.getInstance().get(Key.AccountBy.id, g.getGranteeId());
/* 304 */               group.addElement(DavElements.E_HREF).setText(UrlNamespace.getPrincipalUrl(User.this.mAccount, user));
/*     */             }
/*     */           }
/*     */       } catch (ServiceException se) {
/* 308 */         ZimbraLog.dav.warn("can't get mailbox", se);
/*     */       }
/* 310 */       return group;
/*     */     }
/*     */   }
/*     */   
/*     */   private class CalendarProxyReadFor extends User.ProxyProperty {
/* 315 */     public CalendarProxyReadFor(Account acct) { super(DavElements.E_CALENDAR_PROXY_READ_FOR); }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 319 */       Element proxy = super.toElement(ctxt, parent, true);
/* 320 */       if (nameOnly)
/* 321 */         return proxy;
/* 322 */       ArrayList<Pair<Mountpoint, ZFolder>> mps = getMountpoints(ctxt);
/* 323 */       HashSet<Account> writeProxies = new HashSet();
/* 324 */       HashMap<Account, Element> proxies = new HashMap();
/* 325 */       for (Pair<Mountpoint, ZFolder> folder : mps) {
/*     */         try {
/* 327 */           short rights = ACL.stringToRights(((ZFolder)folder.getSecond()).getEffectivePerms());
/* 328 */           Account owner = Provisioning.getInstance().get(Key.AccountBy.id, ((Mountpoint)folder.getFirst()).getOwnerId());
/* 329 */           if (owner != null)
/*     */           {
/* 331 */             if ((rights & 0x2) > 0) {
/* 332 */               writeProxies.add(owner);
/* 333 */               proxies.remove(owner);
/*     */             }
/* 335 */             if (((rights & 0x2) == 0) && ((rights & 0x1) > 0) && 
/* 336 */               (!writeProxies.contains(owner)) && (!proxies.containsKey(owner))) {
/* 337 */               Element e = DocumentHelper.createElement(DavElements.E_HREF);
/* 338 */               e.setText(UrlNamespace.getPrincipalUrl(User.this.mAccount, owner));
/* 339 */               proxies.put(owner, e);
/*     */             }
/*     */           }
/*     */         } catch (ServiceException se) {
/* 343 */           ZimbraLog.dav.warn("can't convert rights", se);
/*     */         }
/*     */       }
/* 346 */       for (Element e : proxies.values()) {
/* 347 */         proxy.add(e);
/*     */       }
/* 349 */       return proxy;
/*     */     }
/*     */   }
/*     */   
/*     */   private class CalendarProxyWriteFor extends User.ProxyProperty {
/*     */     public CalendarProxyWriteFor(Account acct) {
/* 355 */       super(DavElements.E_CALENDAR_PROXY_WRITE_FOR);
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly) {
/* 359 */       Element proxy = super.toElement(ctxt, parent, true);
/* 360 */       if (nameOnly)
/* 361 */         return proxy;
/* 362 */       ArrayList<Pair<Mountpoint, ZFolder>> mps = getMountpoints(ctxt);
/* 363 */       HashSet<Account> proxies = new HashSet();
/* 364 */       for (Pair<Mountpoint, ZFolder> folder : mps) {
/*     */         try {
/* 366 */           short rights = ACL.stringToRights(((ZFolder)folder.getSecond()).getEffectivePerms());
/* 367 */           if ((rights & 0x2) > 0) {
/* 368 */             Account owner = Provisioning.getInstance().get(Key.AccountBy.id, ((Mountpoint)folder.getFirst()).getOwnerId());
/* 369 */             if (owner == null)
/*     */               continue;
/* 371 */             if (!proxies.contains(owner)) {
/* 372 */               proxy.addElement(DavElements.E_HREF).setText(UrlNamespace.getPrincipalUrl(User.this.mAccount, owner));
/* 373 */               proxies.add(owner);
/*     */             }
/*     */           }
/*     */         } catch (ServiceException se) {
/* 377 */           ZimbraLog.dav.warn("can't convert rights", se);
/*     */         }
/*     */       }
/* 380 */       return proxy;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/User.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */